<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <span class="text1" @click="searchHistories.splice(index, searchHistories.length)"
        >全部删除</span
      >
      <span class="text2" @click="isDeleteShow = false">完成</span>
      <template #right-icon>
        <van-icon name="search" class="toutiao toutiao-shanchu" @click="isDeleteShow = true" />
      </template>
    </van-cell>
    <van-cell
      v-for="(item, index) in searchHistories"
      :key="index"
      :title="item"
      @click="onHistoryClick(item, index)"
    >
      <van-icon name="close" v-show="isDeleteShow" @click="searchHistories.splice(index, 1)" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  data() {
    return {
      isDeleteShow: false // 删除状态
    }
  },
  methods: {
    // 删除每一项
    onHistoryClick(item, index) {
      // 判断是否删除状态下
      if (this.isDeleteShow) {
        this.searchHistories.splice(index, 1) // 删除当前项
      } else {
        this.$emit('Search', item) // 调用父组件方法
      }
    }
  },

  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  }
}
</script>

<style lang="less" scoped>
.search-history {
  .text1,
  .text2 {
    margin: 0 5px;
  }
  .toutiao-shanchu {
    line-height: 50px;
  }
}
</style>
